package com.msc.mc.util;

import java.util.ArrayList;
import java.util.List;

public class ObjectBean extends ImportBean {
	private String oid; 
	private String type;
	private String createRevision;
	private String activateRevision;
	private List<AttributeBean> attributes;
	private List<MeasureBean> measures;
	 
	public String getOid() {
		return oid;
	}
	public void setOid(String oid) {
		this.oid = oid;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getCreateRevision() {
		return createRevision;
	}
	public void setCreateRevision(String createRevision) {
		this.createRevision = createRevision;
	}
	public String getActivateRevision() {
		return activateRevision;
	}
	public void setActivateRevision(String activateRevision) {
		this.activateRevision = activateRevision;
	}
    
	public List<AttributeBean> getAttributes() {
		return attributes;
	}
	public void setAttributes(List<AttributeBean> attributes) {
		this.attributes = attributes;
	}
	
	public MeasureBean addMeasureBean( String name , String type , String nominal , String precision , String quantityType , String units ) {
		if( measures == null ) {
			measures = new ArrayList< MeasureBean >();
		}
		
		MeasureBean measure = new MeasureBean();
		measure.setName(name);
		measure.setType(type);
		measure.setNominal(nominal);
		measure.setPrecision(precision);
		measure.setQuantityType(quantityType);
		measure.setUnits( units );
		measures.add( measure );
		
		return measure;
	}
	
	public AttributeBean addAttributeBean(String name ,String value , String isKey){ 
		if(attributes == null ){
			attributes = new ArrayList<AttributeBean>();
		}
		AttributeBean attr = new AttributeBean();
		attr.setIsKey(isKey);
		attr.setName(name);
		attr.setValue(value);
		attributes.add(attr);
		return attr;
	}
	
	
	public void addAttributeBean(String name, ImportBean importBean , String isKey) {
		addAttributeBean( name , importBean.getId() , isKey );
	}

	public void addAttributeBean(String name, ImportBean[] importBeans, String isKey) {
		StringBuilder ids = new StringBuilder();
		for( int i = 0 ; i < importBeans.length ; i ++ ) {
			if( i > 0 ) {
				ids.append( "," );
			}
			ids.append( importBeans[i].getId() );
		}
		addAttributeBean( name , ids.toString() , isKey );
	}
	
	
	
	@Override
	public String toString() {
		StringBuilder str = new StringBuilder();
		str.append( " <Object ");
		str.append( " id='").append( getId() ).append( "'" );
		str.append( " type='").append( type ).append( "'" );
		if(  oid != null ) {
			str.append( " oid='").append( oid ).append( "'" );
		}
		if(  createRevision != null ) {
			str.append( " createRevision='").append( createRevision ).append( "'" );
		}
		if(  activateRevision != null ) {
			str.append( " activateRevision='").append( activateRevision ).append( "'" );
		}
		str.append( "  >\n");
		if( measures != null ) {
			for( int i = 0 ; i < measures.size() ; i++ ) {
				str.append( measures.get( i ).toString() );
			}
		}
		
		if(attributes!= null && attributes.size() > 0){
			for (int i = 0; i < attributes.size(); i++) {
				str.append( attributes.get(i).toString() );
			}
		}
		str.append( " </Object>\n" );
		return str.toString();
	}
	
	
	
}
